package satir.cell;

import java.util.ArrayList;

import satir.player.Player;

/**
 * The Class Cell.
 * 
 * @version 2.0 March 2012
 * @author Chris Chan
 */
public class Cell {
	
	/** The next cell. */
	private Cell nextCell;
	
	/** The width. */
	private int width;
	
	/** The height. */
	private int height;
	
	/** The array for display. */
	private char cellGround[][];
	
	/** The border horizontal. */
	private char borderHorizontal;
	
	/** The border vertical. */
	private char borderVertical;
	
	/** The players that step on this cell. */
	private ArrayList<Player> players;
	
	/** The cell label. */
	protected String cellLabel;

	/**
	 * Instantiates a new cell.
	 *
	 * @param width the width
	 * @param height the height
	 * @param borderHorizontal the border horizontal
	 * @param borderVertical the border vertical
	 * @param cellLabel the cell label
	 */
	public Cell(int width, int height, char borderHorizontal, char borderVertical, String cellLabel) {
		this.width = width;
		this.height = height;
		this.borderHorizontal = borderHorizontal;
		this.borderVertical = borderVertical;
		this.cellGround = new char[height][width];
		this.cellLabel = cellLabel;
		this.players = new ArrayList<Player>();
		//Fill in the array for display
		for(int i = 0; i<height; i++){
			for(int j=0; j<width; j++){
				if(i==0 || i==height-1){
					this.cellGround[i][j] = this.borderHorizontal;
				}else if(j==0 || j==width-1){
					this.cellGround[i][j] = this.borderVertical;
				}else if(i==height-2){
					if(j>cellLabel.length()){
						this.cellGround[i][j] =' ';
					}else{
						this.cellGround[i][j] = cellLabel.charAt(j-1);
					}
				}else{
					this.cellGround[i][j] = ' ';
				}
			}
		}		
	}

	/**
	 * Gets the height.
	 *
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * Gets the width.
	 *
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * Cell action.
	 *
	 * @param target the target
	 */
	public void cellAction(Player target) {
		System.out.println("Empty Cell!Nothing to do!!!");
	}

	/**
	 * Sets the next cell.
	 *
	 * @param nextCell the new next cell
	 */
	public void setNextCell(Cell nextCell) {
		this.nextCell = nextCell;
	}

	/**
	 * Gets the next cell.
	 *
	 * @return the next cell
	 */
	public Cell getNextCell() {
		return nextCell;
	}

	/**
	 * Display the cell according to the row number.
	 *
	 * @param rowNum the row number
	 */
	public void display(int rowNum) {
		for(int i=0; i<width; i++){
			System.out.print(cellGround[rowNum][i]);
		}
	}
	
	/**
	 * Adds the player when player step on this cell.
	 *
	 * @param player the player
	 */
	public void addPlayer(Player player){
		this.players.add(player);
		boolean isSetFig = false;
		for(int i=1; i<height-2; i++){
			for(int j=1; j<width-1; j++){
				if(cellGround[i][j]==' '){
					cellGround[i][j] = player.getFigure();
					isSetFig = true;
					break;
				}
			}
			if(isSetFig){
				break;
			}
		}
	}
	
	/**
	 * Removes the player when they leave the cell.
	 *
	 * @param player the player
	 */
	public void removePlayer(Player player){
		this.players.remove(player);
		for(int i=1; i<height-2; i++){
			for(int j=1; j<width-1; j++){
				if(cellGround[i][j]==player.getFigure()){
					cellGround[i][j] = ' ';
				}
			}
		}
	}
}